﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.HtmlControls;

public partial class Query : System.Web.UI.Page
{
    DateTime minDate, maxDate;
    string url = "";
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private void GenYearMonth()
    {
        DropDownList2.Items.Clear();
        DropDownList3.Items.Clear();
        DropDownList5.Items.Clear();
        DropDownList6.Items.Clear();
        DropDownList4.Items.Clear();
        DropDownList7.Items.Clear();
        DropDownList2.Items.Add("请选择年");
        DropDownList3.Items.Add("请选择月");
        DropDownList5.Items.Add("请选择年");
        DropDownList6.Items.Add("请选择月");
        DropDownList4.Items.Add("请先选择年月");
        DropDownList7.Items.Add("请先选择年月");
        string sql = "select max(date),min(date) from Sales";
        SqlConnection conn = new SqlConnection(
        ConfigurationManager.ConnectionStrings["testLottery"].ConnectionString);
        conn.Open();
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(sql, conn);
        SqlCommand cmd = new SqlCommand(sql, conn);
        da.Fill(ds);
        if (!ds.Tables[0].Rows[0].IsNull(0))
        {
            maxDate = (DateTime)ds.Tables[0].Rows[0][0];
            minDate = (DateTime)ds.Tables[0].Rows[0][1];
        }
        else
        {
            maxDate = DateTime.Now;
            minDate = DateTime.Now;
        }
        for (int i = minDate.Year; i <= maxDate.Year; i++)
        {
            DropDownList2.Items.Add(i.ToString());
            DropDownList5.Items.Add(i.ToString());
        }
        for (int i = 1; i <= 12; i++)
        {
            DropDownList3.Items.Add(i.ToString());
            DropDownList6.Items.Add(i.ToString());
        }
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedIndex == 1)
        {
            LabelStartTime.Visible = true;
            LabelEndTime.Visible = true;
            DropDownList2.Visible = true;
            DropDownList3.Visible = false;
            DropDownList4.Visible = false;
            DropDownList5.Visible = true;
            DropDownList6.Visible = false;
            DropDownList7.Visible = false;
            GenYearMonth();
        }
        else if (DropDownList1.SelectedIndex == 2)
        {
            LabelStartTime.Visible = true;
            LabelEndTime.Visible = true;
            DropDownList2.Visible = true;
            DropDownList3.Visible = true;
            DropDownList4.Visible = false;
            DropDownList5.Visible = true;
            DropDownList6.Visible = true;
            DropDownList7.Visible = false;
            GenYearMonth();
        }
        else if (DropDownList1.SelectedIndex == 3)
        {
            LabelStartTime.Visible = true;
            LabelEndTime.Visible = true;
            DropDownList2.Visible = true;
            DropDownList3.Visible = true;
            DropDownList4.Visible = true;
            DropDownList5.Visible = true;
            DropDownList6.Visible = true;
            DropDownList7.Visible = true;
            GenYearMonth();
        }
    }

    private bool CheckLeap(int year)
    {
        if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
            return true;
        else return false;
    }

    private int GenDay(int year, int month)
    {
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
        {
            return 31;
        }
        else if (month == 4 || month == 6 || month == 9 || month == 11)
        {
            return 30;
        }
        else if (CheckLeap(year) && month == 2)
        {
            return 29;
        }
        else if (!CheckLeap(year) && month == 2)
        {
            return 28;
        }
        return 0;
    }

    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        int day;
        if (DropDownList3.SelectedIndex != 0)
        {
            if (DropDownList2.SelectedIndex > 0)
            {
                day = GenDay(Convert.ToInt32(DropDownList2.SelectedValue), Convert.ToInt32(DropDownList3.SelectedValue));
                DropDownList4.Items.Clear();
                DropDownList4.Items.Add("请选择日");
            }
            else
            {
                day = 0;
            }
            for (int i = 1; i <= day; i++)
            {
                DropDownList4.Items.Add(i.ToString());
            }
        }
    }
    protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
    {
        int day;
        if (DropDownList2.SelectedIndex != 0)
        {
            if (DropDownList3.SelectedIndex > 0)
            {
                day = GenDay(Convert.ToInt32(DropDownList2.SelectedValue), Convert.ToInt32(DropDownList3.SelectedValue));
                DropDownList4.Items.Clear();
                DropDownList4.Items.Add("请选择日");
            }
            else
            {
                day = 0;
            }
            for (int i = 1; i <= day; i++)
            {
                DropDownList4.Items.Add(i.ToString());
            }
        }
    }
    protected void DropDownList5_SelectedIndexChanged(object sender, EventArgs e)
    {
        int day;
        if (DropDownList6.SelectedIndex != 0)
        {
            if (DropDownList5.SelectedIndex > 0)
            {
                DropDownList7.Items.Clear();
                DropDownList7.Items.Add("请选择日");
                day = GenDay(Convert.ToInt32(DropDownList5.SelectedValue), Convert.ToInt32(DropDownList6.SelectedValue));
            }
            else
            {
                day = 0;
            }
            for (int i = 1; i <= day; i++)
            {
                DropDownList7.Items.Add(i.ToString());
            }
        }
    }
    protected void DropDownList6_SelectedIndexChanged(object sender, EventArgs e)
    {
        int day;
        if (DropDownList5.SelectedIndex != 0)
        {

            if (DropDownList6.SelectedIndex > 0)
            {
                DropDownList7.Items.Clear();
                DropDownList7.Items.Add("请选择日");
                day = GenDay(Convert.ToInt32(DropDownList5.SelectedValue), Convert.ToInt32(DropDownList6.SelectedValue));
            }
            else
            {
                day = 0;
            }
            for (int i = 1; i <= day; i++)
            {
                DropDownList7.Items.Add(i.ToString());
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int bYear, eYear, bMonth, eMonth, bDay, eDay;
        bYear = DropDownList2.SelectedIndex;
        eYear = DropDownList5.SelectedIndex;
        bMonth = DropDownList3.SelectedIndex;
        eMonth = DropDownList6.SelectedIndex;
        bDay = DropDownList4.SelectedIndex;
        eDay = DropDownList7.SelectedIndex;
        if (DropDownList1.SelectedIndex == 0)
        {
            Label1.Text = "请选择时间粒度";
        }
        else if (bYear == 0 || eYear == 0)
        {
            Label1.Text = "请选择年份";
        }
        else if ((bMonth == 0 || eMonth == 0) && DropDownList1.SelectedIndex >= 2)
        {
            Label1.Text = "请选择月份";
        }
        else if ((bDay == 0 || eDay == 0) && DropDownList1.SelectedIndex == 3)
        {
            Label1.Text = "请选择日";
        }
        else if (bYear > eYear)
        {
            Label1.Text = "开始日期大于结束日期";
        }
        else if (DropDownList1.SelectedIndex >= 2 && bYear == eYear && bMonth > eMonth)
        {
            Label1.Text = "开始日期大于结束日期";
        }
        else if (DropDownList1.SelectedIndex == 3 && bYear == eYear && bMonth == eMonth && bDay > eDay)
        {
            Label1.Text = "开始日期大于结束日期";
        }
        else
        {
            Label1.Text = "";
        }
    }
}